#上线配置文件
import os
from .const import *

# 现在BASE_DIR是内部的小的luffy路径
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# 把这个小luffy路径添加到环境变量中
import sys

# sys.path.append(BASE_DIR)
sys.path.insert(0, BASE_DIR)
# apps的路径加入到环境变量中
sys.path.insert(1, os.path.join(BASE_DIR, 'apps'))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.0/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '3xux@3$(h*hnzr**$-fksj8tk6pbu3f(w76e^3z=oqi!xd3y=p'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False

#运行的host, 服务端的地址，买的服务器的公网ip
ALLOWED_HOSTS = ['*']

# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    # 因为apps目录已经被加到环境变量了，所以直接能找到
    'home',
    'user1',
    'order',
    # xadmin主体模块
    'xadmin',
    # 渲染表格模块
    'crispy_forms',
    # 为模型通过版本控制，可以回滚数据
    'reversion',
    # 第三方跨域插件
    'corsheaders',
    'rest_framework',
    'rest_framework_jwt',
    'course',
    #过滤
    'django_filters'
]
MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    # 同源策略
    # 'luffy.utils.corsmiddleware.MyMiddle',
    'corsheaders.middleware.CorsMiddleware',

]

ROOT_URLCONF = 'luffy.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')]
        ,
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'luffy.wsgi.application'

# Database
# https://docs.djangoproject.com/en/3.0/ref/settings/#databases


#跑代码和mysql 是同一台机器，就不需要改
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'luffy',
        'USER': 'luffy',
        'PASSWORD': 'Luffy123?',
        'HOST': '127.0.0.1',
        'PORT': 3306
    }
}

# Password validation
# https://docs.djangoproject.com/en/3.0/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]

# Internationalization
# https://docs.djangoproject.com/en/3.0/topics/i18n/

LANGUAGE_CODE = 'zh-hans'

TIME_ZONE = 'Asia/Shanghai'

USE_I18N = True

USE_L10N = True

USE_TZ = False

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.0/howto/static-files/

STATIC_URL = '/static/'

STATIC_ROOT =os.path.join(BASE_DIR, 'static')

#静态文件配置
# STATICFILES_DIRS = [
#     os.path.join(BASE_DIR, 'static'),
# ]

AUTH_USER_MODEL = 'user1.userInfo'

MEDIA_URL = '/media/'

MEDIA_ROOT = os.path.join(BASE_DIR, 'media')  # 现在的BASEDIR是luffyapi下的luffyapi
# 允许跨域源
CORS_ORIGIN_ALLOW_ALL = True
# 允许跨域的方法
CORS_ALLOW_METHODS = (
    'DELETE',
    'GET',
    'OPTIONS',
    'PATCH',
    'POST',
    'PUT',
    'VIEW',
)

# 允许的请求头
CORS_ALLOW_HEADERS = (
    "accept",
    "accept-encoding",
    "authorization",
    "content-type",
    "dnt",
    "origin",
    "user1-agent",
    "x-csrftoken",
    "x-requested-with",

    # 额外允许的请求头
    'token',

)

# 日志配置
LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'verbose': {
            'format': '%(levelname)s %(asctime)s %(module)s %(lineno)d %(message)s'
        },
        'simple': {
            'format': '%(levelname)s %(module)s %(lineno)d %(message)s'
        },
    },
    'filters': {
        'require_debug_true': {
            '()': 'django.utils.log.RequireDebugTrue',
        },
    },
    'handlers': {
        'console': {
            # 实际开发建议使用WARNING
            'level': 'WARNING',
            'filters': ['require_debug_true'],
            'class': 'logging.StreamHandler',
            'formatter': 'simple'
        },
        'file': {
            # 实际开发建议使用ERROR
            'level': 'INFO',
            'class': 'logging.handlers.RotatingFileHandler',
            # 日志位置,日志文件名,日志保存目录必须手动创建，注：这里的文件路径要注意BASE_DIR代表的是小luffyapi
            'filename': os.path.join(os.path.dirname(BASE_DIR), "logs", "luffy.log"),
            # 日志文件的最大值,这里我们设置300M
            'maxBytes': 300 * 1024 * 1024,
            # 日志文件的数量,设置最大日志数量为10
            'backupCount': 100,
            # 日志格式:详细格式
            'formatter': 'verbose',
            # 文件内容编码
            'encoding': 'utf-8'
        },
    },
    # 日志对象
    'loggers': {
        'django': {
            'handlers': ['console', 'file'],
            'propagate': True,  # 是否让日志信息继续冒泡给其他的日志处理系统
        },
    }
}

import datetime

JWT_AUTH = {
    # 过期时间为 7 天
    'JWT_EXPIRATION_DELTA': datetime.timedelta(days=7),
}

REST_FRAMEWORK = {
    'DEFAULT_THROTTLE_RATES': {
        'sms': '1/m'  # key要跟类中的scop对应  一分钟只能发一次
    },
}

# 缓存配置
CACHES = {
    "default": {
        "BACKEND": "django_redis.cache.RedisCache",  # 指定缓存使用的引擎
        "LOCATION": "redis://127.0.0.1:6379",  # 指定缓存的路径
        "OPTIONS": {  # 参数
            "CLIENT_CLASS": "django_redis.client.DefaultClient",  # django客户端
            "CONNECTION_POOL_KWARGS": {"max_connections": 100}   #连接池的最大连接数
            # "PASSWORD": "123",
        }
    }
}

#上线后必须换成公网地址
#后台基URL
BASE_URL = 'http://139.196.184.56:8000'
#前台基URL
LUFFY_URL='http://139.196.184.56'

#支付宝同步异步回调接口配置
#后台异步回调接口
NOTIFY_URL=BASE_URL+'/order/success/'

#前台同步回调接口，没有 /结尾
RETURN_URL = LUFFY_URL + '/pay/success'
